package com.bxw.forum.service.impl;

import com.bxw.forum.common.AppResult;
import com.bxw.forum.common.ResultCode;
import com.bxw.forum.dao.BoardMapper;
import com.bxw.forum.exception.ApplicationException;
import com.bxw.forum.model.Board;
import com.bxw.forum.service.IBoardService;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    BoardMapper boardMapper;


    /**
     * 获取版块列表
     * @param num
     * @return
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        //1，校验参数
        //2，调用dao层接口查询
        //3，返回数据


        if(num <= 0) {

            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        List<Board> boardList = boardMapper.selectByNum(num);

        return boardList;
    }

    /**
     * 通过版块ID增加对应的文章数量
     * @param id 版块ID
     */
    @Override
    public void addOneArticleCountById(Long id) {

        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        Board board = boardMapper.selectByPrimaryKey(id);

        if(board == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ",board id = " + id);

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() + 1);

        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }

    /**
     * 根据id去查询版块信息
     * @param id
     * @return
     */
    @Override
    public Board selectById(Long id) {
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        Board board = boardMapper.selectByPrimaryKey(id);

        if(board == null) {
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ",board id = " + id);

            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_IS_NULL));
        }

        return board;
    }


    /**
     * 用于在测试时加入版块
     * @param board
     * @return
     */
    @Override
    public int insertBoard(Board board) {
        if(board == null
                || board.getId() == null
                || board.getName() == null) {
            log.warn(ResultCode.FAILED.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }

        //获取上一个版块的sort，根据上一个版块的sort来set当前版块的sort
        Long lastBoardIndex = (Long) (board.getId() - 1);
        Board lastBoard = boardMapper.selectByPrimaryKey(lastBoardIndex);

        if(lastBoard == null || lastBoard.getId() == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }


        Board insertBoard = new Board();
        insertBoard.setId(board.getId());
        insertBoard.setName(board.getName());
        insertBoard.setSort(lastBoard.getSort() + 1);
        insertBoard.setState((byte)0);
        insertBoard.setDeleteState((byte)0);

        Date date = new Date();

        insertBoard.setUpdateTime(date);
        insertBoard.setCreateTime(date);


        int row = boardMapper.insertSelective(insertBoard);

        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_CREATE));
        }

        return 0;
    }

    /**
     * 通过版块ID减少对应的文章数量
     * @param id 版块ID
     */
    @Override
    public void subOneArticleCountById(Long id) {
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        Board board = boardMapper.selectByPrimaryKey(id);

        if(board == null || board.getDeleteState() != 0) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ",board id = " + id);

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() - 1);

        if(updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }

        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }
}
